﻿namespace Site2AppWP8.Config
{
    using System.Collections.Generic;
    using System.Runtime.Serialization;
    using System.Text;

    [DataContract]
    public class StylesConfig
    {
        public StylesConfig()
        {
            this.HiddenElements = new List<string>();
        }

        [DataMember(Name = "setViewport")]
        public bool SetViewport { get; set; }

        public double TargetWidth { get; set; }
        [DataMember(Name = "targetWidth")]
        public string TargetWidthString { get; set; }

        public double TargetHeight { get; set; }
        [DataMember(Name = "targetHeight")]
        public string TargetHeightString { get; set; }

        [DataMember(Name = "supressTouchAction")]
        public bool SupressTouch { get; set; }

        [DataMember(Name = "hiddenElements")]
        public List<string> HiddenElements { get; set; }

        [DataMember(Name = "customCssString")]
        public string CustomCSS { get; set; }

        // When complete, invoke this string into the browser window
        public string GetInvokeString()
        {
            StringBuilder stringBuilder = new StringBuilder();

            // set up the viewport style
            if (this.SetViewport)
            {
                stringBuilder.Append("@-ms-viewport{");
                if (this.TargetWidthString.Length != 0)
                {
                    stringBuilder.Append("width: " + this.TargetWidthString + ";");
                }

                if (this.TargetHeightString.Length != 0)
                {
                    stringBuilder.Append("height: " + this.TargetHeightString + ";");
                }

                stringBuilder.Append("} ");
            }

            // set up the supress touch action
            if (this.SupressTouch)
            {
                stringBuilder.Append("body{-ms-touch-action:none;} ");
            }

            // identify the hidden elements
            if (this.HiddenElements.Count != 0)
            {
                foreach (string s in this.HiddenElements)
                {
                    stringBuilder.Append(s + ", ");
                }

                stringBuilder.Remove(stringBuilder.Length - 2, 2);
                stringBuilder.Append("{display:none !important;} ");
            }

            // Add arbitrary CSS string to the end
            if (this.CustomCSS.Length != 0)
            {
                stringBuilder.Append(this.CustomCSS);
            }

            string returnCSS = "var cssString = '" + stringBuilder.ToString() + "';var styleEl = document.createElement('style');document.body.appendChild(styleEl);styleEl.innerHTML = cssString;";

            return returnCSS;
        }
    }
}
